<template>
  <div class="app-container" ref="appcontainer">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="任务类型" prop="taskType">
        <el-select
          v-model="queryParams.taskType"
          placeholder="请选择任务类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in taskTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
          <!-- <el-option label="委托受理" value="委托受理"/>
          <el-option label="分配试验项目" value="分配试验项目"/>
          <el-option label="检测数据录入" value="检测数据录入"/> -->
          
        </el-select>
      </el-form-item>
      <el-form-item label="委托单编号" prop="gcNumber">
        <el-input
          v-model="queryParams.gcNumber"
          placeholder="请输入委托单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建人" prop="createNickName">
        <el-input
          v-model="queryParams.createNickName"
          placeholder="请输入创建人"
          clearable
          size="small"
        />
      </el-form-item>
      
      <el-form-item label="执行人" prop="cpNickName">
        <el-input
          v-model="queryParams.cpNickName"
          placeholder="请输入执行人"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startCreateTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.startCreateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endCreateTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.endCreateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon=""-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->

<!--          >新增</el-button-->
<!--        >-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-if="false"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :height="tableHeight"
      :data="enterpriseList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="任务编号" align="center" prop="taskNumber" />
      <el-table-column label="任务类型" align="center" prop="taskType">
        <!-- <template slot-scope="scope">
          <dict-tag :options="typeOptions" :value="scope.row.taskType" />
        </template> -->
      </el-table-column>
      <el-table-column
        label="委托单编号"
        align="center"
        prop="gcNumber"

      />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
       {{scope.row.status === "0"  ? "待办" : "已办"}}
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createNickName" />
      <el-table-column label="执行人" align="center" prop="cpNickName" />
      <el-table-column label="任务内容" align="left" prop="taskContent"  width="180"/>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="160"
      />
      <!-- <el-table-column
        label="执行时间"
        align="center"
        prop="executeTime"
        width="160"
      /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            >修改</el-button-->
<!--          >-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, 'comment')"
            >附言</el-button
          >
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            >删除</el-button-->
<!--          >-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改任务对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item
          label="任务编号"
          prop="taskNumber"
          v-show="form.id || title == '附言'"
        >
          <el-input
            v-model="form.taskNumber"
            placeholder="请输入任务编号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
            disabled
          />
        </el-form-item>
        <el-form-item label="任务类型" prop="taskType">
          <el-select
            v-model="form.taskType"
            placeholder="请选择任务类型"
            :disabled="title == '附言'"
          >
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="委托单编号"
          prop="gcNumber"
          v-show="form.id || title == '附言'"
        >
          <el-input
            v-model="form.gcNumber"
            placeholder="请输入委托单编号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
            disabled
          />
        </el-form-item>
        <el-form-item label="执行人" prop="chargePerson">
          <el-select
            v-model="form.chargePerson"
            placeholder="请选择执行人"
            @focus="getPerson"
            :disabled="title == '附言'"
          >
            <el-option
              v-for="dict in personList"
              :key="dict.id"
              :label="dict.name"
              :value="parseInt(dict.id)"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="状态" prop="status">
          <el-select v-model="form.type" placeholder="请选择状态">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="任务内容" prop="taskContent">
          <el-input
            type="textarea"
            v-model="form.taskContent"
            placeholder="请输入任务内容"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
            :disabled="title == '附言'"
          />
        </el-form-item>
        <el-form-item label="附言历史" v-show="form.id || title == '附言'">
          <ul class="list">
            <li v-for="item in commentList" :key="item.id">
              {{ item.postscriptContent }}
            </li>
          </ul>
        </el-form-item>
        <el-form-item
          label="附言"
          prop="postscriptContent"
          v-show="title == '附言'"
        >
          <el-input
            v-model="form.postscriptContent"
            placeholder="请输入附言"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listEnterprise,
  getEnterprise,
  logicDelEnterprise,
  addEnterprise,
  updateEnterprise,
  exportEnterprise,
} from "@/api/enterprise/enterprise";
import {
  listInfo,
  getInfo,
  addInfo,
  updateInfo,
  delInfo,
  getComment,
  addComment,
} from "@/api/task/info/info";
import { listPerson } from "@/api/staff";

export default {
  name: "Enterprise",
  data() {
    return {
      dateRange:[],
      // 附言历史
      commentList: [],
      // 人员列表
      personList: [],
      tableHeight: 0,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业信息表格数据
      enterpriseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 企业类型字典
      typeOptions: [],
      // 状态 1=开业，2=停业字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        code: null,
        type: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "社会信用代码不能为空", trigger: "blur" },
        ],
        entrustCode: [
          { required: true, message: "委托单编号不能为空", trigger: "blur" },
        ],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
        content: [
          { required: true, message: "任务内容不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.calHeight();
    this.getDicts("task_type").then((response) => {
      this.taskTypeOptions = response.data;
    });
    this.getDicts("task_state").then((response) => {
      this.typeOptions = response.data;
    });
  },
  watch: {
    showSearch: "calHeight",
  },
  methods: {
    // 获取人员信息
    getPerson() {
      listPerson().then((res) => {
        this.personList = res.data;
      });
    },
    calHeight() {
      this.$nextTick(() => {
        const rect = this.$refs.appcontainer.getBoundingClientRect();
        let height = this.$refs.queryForm.$el.offsetHeight;
        this.tableHeight = rect.height - height - 120;
      });
    },
    /** 查询企业信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then((response) => {
        this.enterpriseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        code: null,
        type: null,
        contacts: null,
        mobile: null,
        status: 0,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.showSearch = true;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row, type = "normal") {
      this.reset();
      // 查询附言历史
      getComment({ taskId: row.id }).then((res) => {
        this.commentList = res;
      });
      const id = row.id || this.ids;
      getInfo(id).then((response) => {
        this.form = response.data;
        if (type == "comment") {
          this.open = true;
          this.title = "附言";
          this.form.taskId = response.data.id;
          return;
        } else {
          this.open = true;
          this.title = "修改任务";
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            console.log(this.title);

            if (this.title == "附言") {
              addComment(this.form).then((response) => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              updateInfo(this.form).then((response) => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            }
          } else {
            addInfo(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delInfo(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有企业信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportEnterprise(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped>
.list {
  padding: 0;
  margin: 0 20px;
}
</style>
